﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace CustomControls.MessageBox
{
    /// <summary>
    /// 自定义窗体标题栏控件
    /// </summary>
    public class WindowContainer : System.Windows.Controls.Control
    {

        #region 成员变量

        /// <summary>
        /// 标题文本显示控件
        /// </summary>
        TextBlock txtTitle;

        /// <summary>
        /// 最小化Button控件
        /// </summary>
        System.Windows.Controls.Button btnMin;

        /// <summary>
        /// 关闭窗口Button控件
        /// </summary>
        System.Windows.Controls.Button btnClose;

        /// <summary>
        /// 显示窗体内容的容器控件
        /// </summary>
        Border wndContent;

        /// <summary>
        /// 窗口标题区域
        /// </summary>
        Grid gTitle;

        /// <summary>
        /// 窗口标题
        /// </summary>
        string title;

        /// <summary>
        /// 展示的窗口内容控件
        /// </summary>
        UIElement content;

        /// <summary>
        /// 最小化按钮显示状态
        /// </summary>
        Visibility btnMinVisibility;

        #endregion

        #region 构造函数

        public WindowContainer()
        {
            btnMinVisibility = System.Windows.Visibility.Collapsed;
            CloseButtonVisibility = System.Windows.Visibility.Visible;
            title = "窗口标题";
            CanMove = false;
        }

        static WindowContainer()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(WindowContainer), new FrameworkPropertyMetadata(typeof(WindowContainer)));
        }

        #endregion

        #region 属性

        /// <summary>
        /// 获取或设置窗体标题
        /// </summary>
        public string Title
        {
            get { return title; }
            set
            {
                title = value;
                if (this.txtTitle != null)
                {
                    this.txtTitle.Text = title;
                }
            }
        }

        /// <summary>
        /// 获取或设置要显示的窗体内容控件
        /// </summary>       
        public UIElement WindowContent
        {
            set { content = value; }
            get { return content; }
        }

        public Grid GTitle
        {
            get { return gTitle; }
            set { gTitle = value; }
        }
        /// <summary>
        /// 获取或设置最小化按钮是否可见
        /// </summary>
        public Visibility MinButtonVisibility
        {
            get { return btnMinVisibility; }
            set { btnMinVisibility = value; }
        }

        /// <summary>
        /// 获取或设置关闭按钮是否可见
        /// </summary>
        public Visibility CloseButtonVisibility
        {
            get;
            set;
        }

        /// <summary>
        /// 是否启用窗口拖动
        /// </summary>
        public bool CanMove
        {
            get;
            set;
        }

        #endregion

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            txtTitle = Template.FindName("PART_txtTitle", this) as TextBlock;
            txtTitle.Text = title;
            btnMin = Template.FindName("PART_btnMin", this) as System.Windows.Controls.Button;
            btnMin.Visibility = btnMinVisibility;
            btnMin.Click += new RoutedEventHandler(btnMin_Click);
            btnClose = Template.FindName("PART_btnClose", this) as System.Windows.Controls.Button;
            btnClose.Visibility = CloseButtonVisibility;
            btnClose.Click += new RoutedEventHandler(btnClose_Click);
            wndContent = Template.FindName("PART_wndContent", this) as Border;
            wndContent.Child = content;

            gTitle = Template.FindName("gTitle", this) as Grid;
            gTitle.MouseDown += new MouseButtonEventHandler(gTitle_MouseDown);
        }

        public void RemoveVisual()
        {
            wndContent.Child = null;
            content = null;
        }

        #region 事件方法

        private void btnMin_Click(object sender, RoutedEventArgs e)
        {
            Window parentWnd = Window.GetWindow(this);
            parentWnd.Visibility = System.Windows.Visibility.Collapsed;
        }

        private void btnClose_Click(object sender, RoutedEventArgs e)
        {
            Window parentWnd = Window.GetWindow(this);
            parentWnd.Close();
        }

        void gTitle_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (CanMove
                && e.LeftButton == MouseButtonState.Pressed)
            {
                Window parentWnd = Window.GetWindow(this);
                parentWnd.DragMove();
            }
        }

        #endregion
    }
}
